{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('6.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<title>标签查找</title>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 得到的是文档中的第一个匹配的标签\n",
    "sp.title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<body>\n",
       "<div id=\"divid\">\n",
       "<div class=\"div1 div2\">\n",
       "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
       "<a href=\"/5.html\">5_html</a>\n",
       "<p>p4</p>\n",
       "<p>p5</p>\n",
       "<p>p6</p>\n",
       "</div>\n",
       "<div class=\"div1 div3\">\n",
       "<a href=\"/4.html\">4_html</a>\n",
       "<a href=\"/3.html\">3_html</a>\n",
       "<a href=\"/2.html\">2_html</a>\n",
       "<p>p1</p>\n",
       "<p>p2</p>\n",
       "<p>p3</p>\n",
       "</div>\n",
       "</div>\n",
       "</body>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查找并返回第一个匹配的标签\n",
    "sp.find('body')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[<meta charset=\"utf-8\"/>, <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>, <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>]\n"
     ]
    }
   ],
   "source": [
    "metas = sp.find_all('meta')\n",
    "print(len(metas))\n",
    "print(metas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [<a class=\"current\" href=\"/6.html\">6_html</a>]\n"
     ]
    }
   ],
   "source": [
    "## 通过标签属性值来查找\n",
    "elements = sp.find_all('a', {'class':'current'})\n",
    "print(len(elements), elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [<div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>]\n"
     ]
    }
   ],
   "source": [
    "elements = sp.find_all('div', {'class': 'div2'})\n",
    "print(len(elements), elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [<div id=\"divid\">\n",
      "<div class=\"div1 div2\">\n",
      "<a class=\"current\" href=\"/6.html\">6_html</a>\n",
      "<a href=\"/5.html\">5_html</a>\n",
      "<p>p4</p>\n",
      "<p>p5</p>\n",
      "<p>p6</p>\n",
      "</div>\n",
      "<div class=\"div1 div3\">\n",
      "<a href=\"/4.html\">4_html</a>\n",
      "<a href=\"/3.html\">3_html</a>\n",
      "<a href=\"/2.html\">2_html</a>\n",
      "<p>p1</p>\n",
      "<p>p2</p>\n",
      "<p>p3</p>\n",
      "</div>\n",
      "</div>]\n"
     ]
    }
   ],
   "source": [
    "## 通过id查找\n",
    "elements = sp.find_all('div', {'id': 'divid'})\n",
    "print(len(elements), elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method find in module bs4.element:\n",
      "\n",
      "find(name=None, attrs={}, recursive=True, text=None, **kwargs) method of bs4.BeautifulSoup instance\n",
      "    Return only the first child of this Tag matching the given\n",
      "    criteria.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(sp.find)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
